package com.thwj.common.utils;

import com.thwj.common.exception.ServiceException;

import java.lang.reflect.Method;
import java.util.LinkedHashMap;
import java.util.Map;

public class EnumUtil {

    public static Map<Integer,String> getEnum(String className) {
        try {
            Class<Enum> c = (Class<Enum>) Class.forName(className);
            return EnumUtil(c);
        }catch (ClassNotFoundException e){
            throw new ServiceException("枚举不存在");
        }
    }
    private static Map<Integer,String> EnumUtil(Class<Enum> em) {
        try {
            Method toCode = em.getMethod("getValue");
            Method toName = em.getMethod("getName");
            //得到enum的所有实例
            Object[] objs = em.getEnumConstants();
            Map<Integer,String> reMap = new LinkedHashMap<>();
            for (Object obj : objs) {
                reMap.put((Integer)toCode.invoke(obj),(String)toName.invoke(obj));
            }
            return reMap;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        System.out.println(getEnum("com.thwj.project.biz.enums.AccountInoutStatusEnum"));
//        System.out.println(cn.hutool.core.util.EnumUtil.getEnumMap(AccountInoutStatusEnum.class));
    }
}
